<template>
  <div>

    <!-- 1.定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-book :to="{ path: '/home' }">首页</el-breadcrumb-book>
      <el-breadcrumb-book>{{"->"}}用户管理</el-breadcrumb-book>
      <el-breadcrumb-book>{{"->"}}用户列表</el-breadcrumb-book>
    </el-breadcrumb>

    <!-- 2.定义卡片视图 -->
    <el-card class="box-card">

       <!-- 3.定义栅格的一行 :gutter="20"定义行间距   :span="9" 占位符  -->
       <el-row :gutter="20">
         <el-col :span="9">
            <!-- 3.定义搜索框 -->
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable  @clear="getUserList">
              <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
            </el-input>
        </el-col>
        <div align="right" style="font-size: 20px;">
          全站用户为:
          <input type="text" v-model="total" style="width: 60px; height: 30px; font-size: 20px;" disabled></input>
          人
        </div>
      </el-row>

       <!-- 用户数据表格展现  border 边框线属性  -->
      <el-table :data="userList" border stripe>
          <el-table-column type="index" align="center" label="序号" width="80px"></el-table-column>
          <el-table-column prop="username" align="center" label="用户名" width="120px"></el-table-column>
          <el-table-column prop="role" align="center" label="身份" width="100px"></el-table-column>
          <el-table-column prop="phone" align="center" label="电话" width="150px"></el-table-column>
          <el-table-column prop="down_right" align="center" label="下载权限" width="100px">
            <template slot-scope="scope">
               <el-switch v-model="scope.row.downRight" @change="updateDown(scope.row)"
                 active-color="#13ce66" inactive-color="#ff4949">
               </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="up_right" align="center" label="上传权限" width="100px">
            <template slot-scope="scope">
               <el-switch v-model="scope.row.upRight" @change="updateUp(scope.row)"
                 active-color="#13ce66" inactive-color="#ff4949">
               </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="email" align="center" label="邮箱"></el-table-column>
          <el-table-column prop="created" align="center" label="注册时间">
            <template slot-scope="scope">
              <!-- {{scope.row.price | priceFormat}} -->
              {{scope.row.created | dateYMDHMSFormat}}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-chat-dot-round" size="medium" @click="sendMessageBtn(scope.row)"></el-button>
              <el-button type="danger" icon="el-icon-delete" size="medium" @click="deleteUser(scope.row)"></el-button>
            </template>
          </el-table-column>
      </el-table>

       <!-- 定义分页组件 关于分页插件的属性说明
            1.@size-change 如果分页中每页实现的条数发生变化 则会触发这个该事件
            2.@current-change 当页数发生改变时,触发该函数
            3.current-page 当前选中的页
            4.page-sizes   页面数据展现的选项
            5.page-size    默认选中的页数
            6.layout    分页中展现的数据种类
            7.total     记录总数
       -->
        <el-pagination
             @size-change="handleSizeChange"
             @current-change="handleCurrentChange"
             :current-page="queryInfo.pageNum"
             :page-sizes="[5, 10, 20, 40]"
             :page-size="queryInfo.pageSize"
             layout="total, sizes, prev, pager, next, jumper"
             :total="total">
        </el-pagination>
    </el-card>



    <!-- 定义发送消息页面 当对话框关闭时,将表格数据重置-->
    <el-dialog title="发送信息" :visible.sync="sendDialogVisible" width="50%" @close="closeSendDialog">

      <el-form :model="sendMsg" ref="sendMsgRef" label-width="100px" class="demo-ruleForm">
        <!-- 定义消息发送边框-->
        <el-input type="textarea" :rows="5" placeholder="请输入内容" v-model="sendMsg.message"></el-input>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="sendDialogVisible = false" >取 消</el-button>
        <el-button type="primary" @click="sendMessage">确 定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>

  export default {
    data(){

      return {
        queryInfo: {
          query: '',
          pageNum: 1,
          pageSize: 5
        },
        userList: [],
        //记录总数是数值类型
        total: 0,
        dialogVisible: false,
        sendMsg: {
          send: '',
          rece: '',
          message: '',
        },

        sendDialogVisible: false,

      }

    },
    methods: {
      async getUserList(){
        const {data: result} = await this.$http.get('/user/list',{
           params: this.queryInfo
        })
        if(result.status !== 200) return this.$message.error("用户列表查询失败")
        this.userList = result.data.rows
        this.total = result.data.total
        console.log("总记录数:"+this.total)
      },
      async updateDown(user){
        const {data: result} = await this.$http.put(`/user/downRight/${user.id}/${user.downRight}`)
        if(result.status !== 200) return this.$message.error("用户下载权限修改失败!")
        this.$message.success("用户下载权限修改成功!")
      },
      async updateUp(user){
        const {data: result} = await this.$http.put(`/user/upRight/${user.id}/${user.upRight}`)
        if(result.status !== 200) return this.$message.error("用户上传权限修改失败!")
        this.$message.success("用户上传权限修改成功!")
      },
      handleSizeChange(pageSize){
        //console.log("每页展现的条数"+pageSize)
        this.queryInfo.pageSize = pageSize
        this.getUserList()
      },
      handleCurrentChange(pageNum){
        //console.log("页数:"+pageNum)
        this.queryInfo.pageNum = pageNum
        this.getUserList()
      },
      closeDialog(){
        //重置表格数据
        this.$ref.addUserRef.resetFields()
      },
      async sendMessageBtn(user){
        this.sendMsg.send = window.sessionStorage.getItem('userId')
        this.sendMsg.rece = user.id
        this.sendDialogVisible = true

      },
      closeSendDialog(){
        //重置表格数据
        this.$refs.sendMsgRef.resetFields()
        this.sendDialogVisible = false
      },
      sendMessage(){
        this.$refs.sendMsgRef.validate(async valid => {
          console.log(this.sendMsg)
          const {data: result} = await this.$http.post('/msg/sendMsg',this.sendMsg)
          if(result.status !== 200) return this.$message.error("发送消息失败")
          return this.$message.success("消息发送成功")
          //关闭对话框
          this.$refs.sendMsgRef.resetFields()
          this.sendDialogVisible = false

        })
      },

      async deleteUser(user){
         //1.消息确认框
         const result =  await this.$confirm('此操作将永久删除 '+user.username+', 是否继续?', '提示', {
                   confirmButtonText: '确定',
                   cancelButtonText: '取消',
                   type: 'warning'
                 }).catch(error => error)

         //如果确认  confirm  如果取消 cancel
         if(result !== 'confirm'){
            return this.$message.info("取消删除")
         }
           const {data: result2} = await this.$http.delete(`/user/${user.id}`)
          if(result2.status !== 200) return this.$message.error("删除失败")
          this.$message.success("删除成功")
          //重新加载 数据
          this.getUserList()
      }
    },
    //利用钩子函数实现数据查询
    mounted(){
      this.getUserList()
    }
  }
</script>


<style lang="less" scoped>

</style>
